﻿using Fake_Shop_WebApi.DataBase;
using Fake_Shop_WebApi.Models;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Fake_Shop_WebApi.Services
{
    public class TouristRouteRepository : ITouristRouteRepository
    {
        private readonly AppDbContext _context;
        public TouristRouteRepository(AppDbContext context)
        {
            _context = context;
        }

        public async Task<TouristRoute> GetTouristRouteAsync(Guid touristRouteId)
        {
            return await _context.TouristRoutes.Include(t => t.TouristRoutePictures).FirstOrDefaultAsync(x => x.Id == touristRouteId);
        }

        public async Task<IEnumerable<TouristRoute>> GetTouristRoutesAsync(string keyword, string ratingOperator, int? ratingValue)
        {
            IQueryable<TouristRoute> result = _context
                .TouristRoutes.Include(t => t.TouristRoutePictures);
            if (!string.IsNullOrWhiteSpace(keyword))
            {
                keyword = keyword.Trim();
               result= result.Where(t => t.Title.Contains(keyword));
            }
            if (ratingValue >= 0)
            {
                switch (ratingOperator)
                {
                    case"largerThan":
                        result = result.Where(t => t.Rating >= ratingValue);
                        break;
                    case "lessThan":
                        result = result.Where(t => t.Rating <= ratingValue);
                        break;
                    case "equalTo":
                        result = result.Where(t => t.Rating == ratingValue);
                        break;
                }
            }
            return await result.ToListAsync();
        }
        public async Task<bool> GetTouristRouteExistsAsync(Guid touristRouteId)
        {
            return await _context.TouristRoutePictures.AnyAsync(t => t.TouristRouteId == touristRouteId);
        }
        public async Task<IEnumerable<TouristRoutePicture>> GetTouristRoutePicturesAsync(Guid touristRouteId)
        {
            return await _context.TouristRoutePictures.Where(t => t.TouristRouteId == touristRouteId).ToListAsync();
        }
        public async Task<TouristRoutePicture> GetPictureAsync(int pictId)
        {
            return await _context.TouristRoutePictures.Where(p => p.Id == pictId).FirstOrDefaultAsync();
        }
        public void AddTouristRoutePicture(TouristRoute touristRoute)
        {
            if (touristRoute == null)
            {
                //数据异常
                throw new ArgumentNullException(nameof(touristRoute));
            }
            _context.TouristRoutes.Add(touristRoute);
        }
        public void AddTouristRoutePictures(Guid touristRouteId, TouristRoutePicture touristRoutePicture)
        {
            if (touristRouteId == Guid.Empty)
            {
                throw new ArgumentNullException(nameof(touristRouteId));
            }
            if(touristRoutePicture == null)
            {
                throw new ArgumentNullException(nameof(touristRoutePicture));
            }
            touristRoutePicture.TouristRouteId = touristRouteId;
            _context.TouristRoutePictures.Add(touristRoutePicture);
        }
       public void DeleteTouristRoute(TouristRoute touristRoute)
        {
            _context.TouristRoutes.Remove(touristRoute);
        }
        public void DeleteTouristRoutePictures(TouristRoutePicture touristRoutePicture)
        {
            
             _context.TouristRoutePictures.Remove(touristRoutePicture);
           
        }

        public async Task<IEnumerable<TouristRoute>> GetTouristRouteByIdsAsync(IEnumerable<Guid> Ids)
        {
            return await _context.TouristRoutes.Where(t => Ids.Contains(t.Id)).ToListAsync();
        }
        public  void DeleteTouristRoutes(IEnumerable<TouristRoute> touristRoutes)
        {
            _context.TouristRoutes.RemoveRange(touristRoutes);
        }
        public async Task<ShoppingCart> GetShoppingCartByUserIdAsync(string UserId)
        {
            //Include 返回其关联实体
            return await _context.ShoppingCarts.Include(s => s.User)//连接user表
                .Include(s => s.ShoppingCartItem).ThenInclude(li => li.TouristRoute)//连接LineItem表
                .Where(x => x.UserId == UserId)
                .FirstOrDefaultAsync();//找出指定用户的购物车数据
        }
        public async Task CreateShoppingCartAsync(ShoppingCart shoppingCart)
        {
            await _context.ShoppingCarts.AddAsync(shoppingCart);
        }
        public async Task AddShoppingCartItem(LineItem lineItem)
        {
            await _context.LineItems.AddAsync(lineItem);
        }
       public async Task<LineItem> GetShoppingCartItemAsync(int lineId)
        {
            return await _context.LineItems.Where(li => li.Id == lineId).FirstOrDefaultAsync();
        }
        public void DeleteShopingCart(LineItem lineItem)
        {
            _context.LineItems.Remove(lineItem);
        }
       public async Task<IEnumerable<LineItem>> GeshoppingCartsByIdListAsync(IEnumerable<int> ids)
        {
            return await _context.LineItems.Where(li => ids.Contains(li.Id)).ToListAsync();
        }
        public void DeleteShoppingCartItems(IEnumerable<LineItem> lines)
        {
            _context.LineItems.RemoveRange(lines);
        }
        public async Task AddOrderAsync(Order order)
        {
            await _context.Orders.AddAsync(order);
        }
        public async Task<ICollection<Order>> GetOrderAsync(string userId)
        {
            return await _context.Orders.Where(o => o.UserId==userId).ToListAsync();
        }
       public async Task<Order> GetOrderByIdAsync(Guid orderId)
        {
           return await _context.Orders.Include(o => o.OrderItem).ThenInclude(li => li.TouristRoute)
                .Where(o => o.Id == orderId).FirstOrDefaultAsync();
        }
        public async Task<bool> SaveAsync()
        {
            return (await _context.SaveChangesAsync() >= 0);
        }
    }
}
